<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * pc端登录接口
 * @author liubin-bt
 * @property User_model $User_model
 */
class Login extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->config->load('weixin');
    }
    public function goWeixin(){
        $appid = $this->config->item('appid');
        $backurl = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/Login/wxLogin';
        $url = "https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$backurl&response_type=code&scope=snsapi_login&state=".time();
        //跳转到微信
        header('Location: '.$url, TRUE);
    }
    
    /**
     * 微信回调登录地址
     */
    public function wxLogin(){
        $code = trim($_GET['code']);
        $appid = $this->config->item('appid');
        $secret = $this->config->item('secret');
        //通过code取openid和access_token
        $accessurl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
        $accessjson = file_get_contents($accessurl);
        if( !$accessjson ){
            echo json_encode(['status'=>-1, 'msg'=>'用户access_token请求失败']);
            exit;
        }
        $accessinfo = json_decode($accessjson, true);
        if( !isset($accessinfo['access_token']) ){
            echo json_encode(['status'=>-2, 'msg'=>'无法获取用户access_token信息']);
            exit;
        }
        //通过access_token和openid获取用户信息
        $access_token = $accessinfo['access_token'];
        $openid = $accessinfo['openid'];
        $unionid = $accessinfo['unionid'];
        $userurl = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid;
        $userjson = file_get_contents($userurl);
        if( !$userjson ){
            echo json_encode(['status'=>-3, 'msg'=>'获取用户信息请求失败']);
            exit;
        }
        $userinfo = json_decode($userjson, true);
        if( !isset($userinfo['nickname']) ){
            echo json_encode(['status'=>-4, 'msg'=>'解析用户信息失败']);
            exit;
        }
        $this->load->model('User_model');
        $reg_user = $this->User_model->getUserByUnionid($unionid);
        if( !$reg_user ){
            $userdata['user_name'] = $userinfo['nickname'];
            $userdata['photo'] = $userinfo['headimgurl'];
            $userdata['unionid'] = $unionid;
            $userdata['openid'] = $openid;
            $userdata['corp_id'] = 1;
            $userdata['group_id'] = 0;
            $userdata['ctime'] = time();
            $uid = $this->User_model->insert($userdata);
            if( $uid ){
                $userdata['uid'] = $uid;
                $reg_user = $userdata;
                unset($reg_user['openid']);
                unset($reg_user['unionid']);
            }
        } else {
            $reg_user['uid'] = $reg_user['id'];
            unset($reg_user['id']);
        }
        $this->session->user = $reg_user;
        
        header('Location: http://'.$_SERVER['HTTP_HOST'].'/public/index.html#/canlender', TRUE);
    }
}